﻿using Dapper;
using Publicuse.Entity;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq.Expressions;

namespace Publicuse.Interface
{
    /// <summary>
    /// https://dapper-tutorial.net/dapper
    /// </summary>
    public interface IBaseDapperService
    {
        #region 查询 
        /// <summary>
        /// 查询所有的数据
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="connectionType"></param>
        /// <param name="msg"></param>
        /// <returns></returns>
        public List<T> GetAll<T>(ConnectionTypeEnum connectionType,out string msg) where T : class;

        /// <summary>
        /// 根据SQL查询数据
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="sql"></param>
        /// <param name="connectionType"></param>
        /// <param name="msg"></param>
        /// <returns></returns>
        public List<T> Query<T>(string sql, ConnectionTypeEnum connectionType,out string msg) where T : class;

        /// <summary>
        /// 根据条件查询
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="expression"></param>
        /// <param name="connectionType"></param>
        /// <param name="msg"></param>
        /// <returns></returns>
        public IEnumerable<T> Query<T>(Expression<Func<T, bool>> expression, ConnectionTypeEnum connectionType,out string msg) where T : class;

        /// <summary>
        /// 通过编号查询
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="id"></param>
        /// <param name="connectionType"></param>
        /// <param name="msg"></param>
        /// <returns></returns>
        public T Find<T>(int id, ConnectionTypeEnum connectionType,out string msg) where T : BaseModel<long>;

        /// <summary>
        /// 在一个查询中处理多个结果
        /// </summary>
        /// <param name="sql"></param>
        /// <param name="connectionType"></param>
        /// <param name="parameters"></param>
        /// <param name="msg"></param>
        /// <param name="action"></param>
        public void QueryMultiple(string sql, ConnectionTypeEnum connectionType, object parameters, out string msg, Action<SqlMapper.GridReader> action);

        /// <summary>
        /// 根据sql获取数据
        /// </summary>
        /// <param name="sql"></param>
        /// <param name="connectionType"></param>
        /// <param name="msg"></param>
        /// <param name="parameters"></param>
        /// <returns></returns>
        public DataTable GetDataTable(string sql, ConnectionTypeEnum connectionType, out string msg, object parameters);

        /// <summary>
        /// 根据sql获取数据
        /// </summary>
        /// <param name="sql"></param>
        /// <param name="connectionType"></param>
        /// <param name="msg"></param>
        /// <param name="parameters"></param>
        /// <returns></returns>
        public DataSet GetDataSet(string sql, ConnectionTypeEnum connectionType, out string msg, object parameters);

        /// <summary>
        /// 执行SQL
        /// </summary>
        /// <param name="sql"></param>
        /// <param name="connectionType"></param>
        /// <param name="msg"></param>
        /// <param name="parameters"></param>
        /// <returns></returns>
        public bool ExecuteSql(string sql, ConnectionTypeEnum connectionType, out string msg, object parameters);

        /// <summary>
        ///  分页查询
        ///  Nuget：Install-Package Dapper.Common -Version 1.5.0   扩展 
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <typeparam name="S"></typeparam>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="where"></param>
        /// <param name="orderby"></param>
        /// <param name="connectionType"></param>
        /// <param name="msg"></param>
        /// <returns></returns>
        public PageResult<T> GetPageList<T, S>(int pageIndex, int pageSize, Expression<Func<T, bool>> where, Expression<Func<T, S>> orderby, ConnectionTypeEnum connectionType,out string msg) where T : class;
        #endregion

        #region 增加 
        /// <summary>
        /// 增加实体 
        /// nuget引入:Z.Dapper.Plus
        /// </summary>
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="t"></param>
        /// <param name="connectionType"></param>
        /// <param name="msg"></param>
        /// <returns></returns>
        public T Insert<T>(T t, ConnectionTypeEnum connectionType,out string msg) where T : BaseModel<long>;

        /// <summary>
        /// 批量添加
        /// Z.Dapper.Plus
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="list"></param>
        /// <param name="connectionType"></param>
        /// <param name="msg"></param>
        /// <returns></returns>
        public List<T> InsertBatch<T>(List<T> list, ConnectionTypeEnum connectionType,out string msg) where T : BaseModel<long>;
        #endregion

        #region 修改
        /// <summary>
        /// 修改实体
        /// 增加 nuget引入:Z.Dapper.Plus
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="t"></param>
        /// <param name="connectionType"></param>
        /// <param name="msg"></param>
        /// <returns></returns>
        public T Update<T>(T t, ConnectionTypeEnum connectionType,out string msg) where T : BaseModel<long>;

        /// <summary>
        /// 批量修改
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="list"></param>
        /// <param name="connectionType"></param>
        /// <param name="msg"></param>
        /// <returns></returns>
        public IEnumerable<T> UpdateInsertBatch<T>(IEnumerable<T> list, ConnectionTypeEnum connectionType,out string msg) where T : BaseModel<long>;

        #endregion

        #region 删除

        /// <summary>
        /// 根据ID删除
        /// 增加 nuget引入:Z.Dapper.Plus
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="id"></param>
        /// <param name="connectionType"></param>
        /// <param name="msg"></param>
        /// <returns></returns>
        public bool Delete<T>(int id, ConnectionTypeEnum connectionType, out string msg) where T : BaseModel<long>,new ();


        /// <summary>
        /// 根据条件删除
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="where"></param>
        /// <param name="connectionType"></param>
        /// <param name="msg"></param>
        /// <returns></returns>
        public bool Delete<T>(Expression<Func<T, bool>> where, ConnectionTypeEnum connectionType,out string msg) where T : BaseModel<long>;

        #endregion 
    }
}
